using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using AK.Wwise.TreeView;

namespace AudioStudio.Editor
{
	[CustomPropertyDrawer(typeof(AudioEvent))]
	public class AudioEventDrawer : AudioBaseTypeDrawer
    {
        public override void SetupSerializedProperties(SerializedProperty property)
        {
            m_objectType = AkWwiseProjectData.WwiseObjectType.EVENT;
            m_typeName = "Event";            
        }

        protected override void OnPickerChanged(SerializedProperty property, TreeViewItem treeItem)
        {
            Guid guid = Guid.Empty;
            if (treeItem != null)
            {
                guid = new Guid((treeItem.DataContext as AkWwiseTreeView.AkTreeInfo).Guid);
            }
            var guidProperty = property.FindPropertyRelative("valueGuid.Array");
            AudioUtility.SetByteArrayProperty(guidProperty, guid.ToByteArray());

            var e = AkWwiseProjectData.Instance.FindEvent(guid);

            if (e != null)
            {
                property.FindPropertyRelative("ID").intValue = e.ID;
                property.FindPropertyRelative("name").stringValue = e.Name;
            }
            else
            {
                property.FindPropertyRelative("ID").intValue = 0;
                property.FindPropertyRelative("name").stringValue = string.Empty;
            }
            property.serializedObject.ApplyModifiedProperties();
        }

    }
}